package cn.stylefeng.guns.modular.hp.controller;

import cn.stylefeng.guns.config.properties.HpProperties;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.hp.domain.FriendlyLinkInfoDO;
import cn.stylefeng.guns.modular.hp.model.FriendlyLinkInfo;
import cn.stylefeng.guns.modular.hp.service.IFriendlyLinkInfoService;
import cn.stylefeng.guns.modular.hp.warpper.FriendlyLinkInfoWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 友情链接控制器
 *
 * @author xzqiang
 * @date 2018-12-08
 */
@Controller
@RequestMapping("/hp/friendlyLink")
public class FriendlyLinkInfoController extends BaseController {

    private String PREFIX = "/hp/friendlyLink/";
    
    @Autowired
    private HpProperties hpProperties;

    @Autowired
    private IFriendlyLinkInfoService friendlyLinkInfoService;
    
    /**
     * 跳转到友情链接列表首页
     */
    @RequestMapping("")
    public String index() {
    	String htmlPath = "friendlyLink.html";
        return PREFIX + htmlPath;
    }

    /**
     * 跳转到添加友情链接
     */
    @RequestMapping("/friendlyLink_add")
    public String friendlyLinkAdd() {
    	String htmlPath = "friendlyLink_add.html";
        return PREFIX + htmlPath;
    }

    /**
     * 跳转到修改友情链接
     */
    @RequestMapping("/friendlyLink_update/{infoId}")
    public String friendlyLinkUpdate(@PathVariable String infoId, Model model) {
    	FriendlyLinkInfo info = this.friendlyLinkInfoService.selectById(infoId);
    	
    	FriendlyLinkInfoDO infoDO = new FriendlyLinkInfoDO();
    	
    	infoDO.setId(info.getId());
    	infoDO.setName(info.getName());
    	infoDO.setLinkUrl(info.getLinkUrl());
    	infoDO.setPicName(info.getPicName());
    	infoDO.setPicUrl(info.getPicUrl());
    	infoDO.setPri(info.getPri());
    	
        model.addAttribute("info", infoDO);
        LogObjectHolder.me().set(info);

        String htmlPath = "friendlyLink_edit.html";
    	
        return PREFIX + htmlPath;
    }


    /**
     * 获取友情链接列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.friendlyLinkInfoService.list(condition);
        return super.warpObject(new FriendlyLinkInfoWrapper(list));
    }

    /**
     * 新增友情链接
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增友情链接", key = "id")//, dict = NoticeMap.class
    public Object add(FriendlyLinkInfo friendlyLinkInfo) {
        if (ToolUtil.isOneEmpty(friendlyLinkInfo, friendlyLinkInfo.getName(), friendlyLinkInfo.getPri(), friendlyLinkInfo.getPicUrl(), friendlyLinkInfo.getLinkUrl())) { //, topMenuInfo.getPageContentId()
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        
        //新增友情链接
        FriendlyLinkInfo newFriendLink = new FriendlyLinkInfo();
        newFriendLink.setName(friendlyLinkInfo.getName());
        newFriendLink.setLinkUrl(friendlyLinkInfo.getLinkUrl());
        newFriendLink.setPicName(friendlyLinkInfo.getPicUrl());
        newFriendLink.setPicUrl(friendlyLinkInfo.getPicUrl());
        newFriendLink.setPri(friendlyLinkInfo.getPri());
        newFriendLink.setCreater(ShiroKit.getUser().getId());
        newFriendLink.setCreatedTime(new Date());
        newFriendLink.insert();
        return SUCCESS_TIP;
    }

    /**
     * 删除友情链接
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除友情链接", key = "id")//, dict = NoticeMap.class
    public Object delete(@RequestParam String id) {

        this.friendlyLinkInfoService.deleteById(id);

        return SUCCESS_TIP;
    }

    /**
     * 修改友情链接
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改友情链接", key = "id")//, dict = NoticeMap.class
    public Object update(FriendlyLinkInfoDO infoDO) {
    	if (ToolUtil.isOneEmpty(infoDO, infoDO.getId(), infoDO.getName(), infoDO.getPri(), infoDO.getPicUrl(), infoDO.getLinkUrl())) { 
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
    	FriendlyLinkInfo oldTopMenuInfo = this.friendlyLinkInfoService.selectById(infoDO.getId());
    	
    	oldTopMenuInfo.setName(infoDO.getName());
    	oldTopMenuInfo.setLinkUrl(infoDO.getLinkUrl());
    	oldTopMenuInfo.setPicName(infoDO.getPicUrl());
    	oldTopMenuInfo.setPicUrl(infoDO.getPicUrl());
    	oldTopMenuInfo.setPri(infoDO.getPri());
    	oldTopMenuInfo.updateById();
        return SUCCESS_TIP;
    }

}
